package com.uam.core.controller;

import com.uam.core.business.RouteStatusInfo;
import com.uam.core.cache.PointCacheConfig;
import com.uam.core.dao.elasticsearch.ElasticsearchDao;
import com.uam.core.dao.mysql.TaskDao;
import com.uam.core.entity.constant.RedisConstants;
import com.uam.core.entity.enums.ElasticsearchIndices;
import com.uam.core.entity.model.TaskModel;
import com.uam.core.entity.model.TaskQueryResult;
import com.uam.core.entity.model.entity.uitls.DateUtil;
import com.uam.core.scheduler.PointInfoScheduler;
import com.uam.core.service.RouteService;
import com.uam.core.service.SectorService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.uam.core.lk.utils.ResponseResult;
import com.uam.core.lk.utils.annotation.BaseErrorCode;
import com.uam.core.service.DatasService;

import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 数据管理
 *
 * @author lishf
 */
@Slf4j
@RestController
@RequestMapping("/uam/route/datas")
public class DatasController {

    @Autowired
    private DatasService datasService = null;

    @Autowired
    private ElasticsearchDao elasticsearchDao;

	@Autowired
	private TaskDao taskDao;

	@Autowired
	private PointCacheConfig pointCacheConfig;

	@Autowired
	RedisTemplate<String, Object> redisTemplate;
	@Autowired
	private SectorService sectorService = null;

	@Autowired
	private RouteStatusInfo routeStatusInfo;

	private long interval=5000L;

	@Autowired
	private RouteService routeService;


    @GetMapping(value = "/init", produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE,
            MediaType.APPLICATION_OCTET_STREAM_VALUE, MediaType.TEXT_HTML_VALUE})
    public ResponseResult<Boolean> init() {
        long startTime = System.currentTimeMillis();
        try {
            this.datasService.initDatas();
            ResponseResult<Boolean> response = new ResponseResult<Boolean>(Boolean.TRUE);
            DatasController.log.info("base_station detail cost:{}ms", System.currentTimeMillis() - startTime);
            return response;
        } catch (Throwable e) {
            DatasController.log.error("初始化数据异常", e);
            return new ResponseResult<Boolean>(BaseErrorCode.SpringExceptionErrorCode);
        }
    }

    @GetMapping(value = "/restart/tesk", produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE,
            MediaType.APPLICATION_OCTET_STREAM_VALUE, MediaType.TEXT_HTML_VALUE})
    public ResponseResult<Boolean> restartTask(String ids,Integer status,Long interval) {
        if ("".equals(ids) || ids == null) {
            return new ResponseResult<Boolean>(BaseErrorCode.SpringExceptionErrorCode);
        }

		Long times = System.currentTimeMillis();
        String[] idsArr = ids.split(",");
        for (int i = 0; i < idsArr.length; i++) {
            try {
				Integer sourceTaskId=Integer.parseInt(idsArr[i]);
                Map<String, Object> data = new HashMap<String, Object>();
                Map<String, Object> deleteData = new HashMap<>();
                deleteData.put("task_id", sourceTaskId);
                long row= elasticsearchDao.delByQuery(ElasticsearchIndices.ROUTE_COMPONENT_INDEX.getName(), data, null, null, null);
				taskDao.restartTask(sourceTaskId,times,status);
                if(interval!=null) {
					times = times + interval;
				}

				PointInfoScheduler.deletePointInfoDo(sourceTaskId);
				pointCacheConfig.del(sourceTaskId);
				redisTemplate.delete(RedisConstants.ROUTE_POINTS + sourceTaskId);
				redisTemplate.delete(RedisConstants.TASK_MODEL_INFO + sourceTaskId);
				redisTemplate.delete(RedisConstants.ROUTE_AIR_TASK + sourceTaskId);
				sectorService.getStatCount();
				PointInfoScheduler.signalStrength=routeService.getAllBaseStations();
				PointInfoScheduler.sectorStatModelMap.remove(sourceTaskId);

            } catch (Throwable e) {
                DatasController.log.error("初始化数据异常", e);
            }
        }
        ResponseResult<Boolean> response = new ResponseResult<Boolean>(Boolean.TRUE);
        return response;

    }


}
